{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **Note**: This example does not output correctly in Jupyter notebook.\n",
    "\n",
    "In this example, we are creating the spinner output but using a implementation with Python `asyncio` coroutines. \n",
    "\n",
    "### spin()\n",
    "\n",
    "This function accepts a message and then outputs that message with a spinning bar as it is running. \n",
    "\n",
    "### slow_function()\n",
    "\n",
    "This function purposefully uses `yield from` `asyncio.sleep(...)` to pause the loop execution at this point for three seconds. \n",
    "\n",
    "### supervisor()\n",
    "\n",
    "This function creates an `asyncio` Future named `spinner`. And then does `yield from` the `slow_function()` to wait three seconds. After the three seconds have occurred it then calls `.cancel` on the spinner Future and returns the result of the `spinner()` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "spinner object: <Task pending coro=<spin() running at <ipython-input-1-1c6c6542c063>:13>>\n",
      "| thinking!\b\b\b\b\b\b\b\b\b\b\b/ thinking!\b\b\b\b\b\b\b\b\b\b\b- thinking!\b\b\b\b\b\b\b\b\b\b\b\\ thinking!\b\b\b\b\b\b\b\b\b\b\b| thinking!\b\b\b\b\b\b\b\b\b\b\b/ thinking!\b\b\b\b\b\b\b\b\b\b\b- thinking!\b\b\b\b\b\b\b\b\b\b\b\\ thinking!\b\b\b\b\b\b\b\b\b\b\b| thinking!\b\b\b\b\b\b\b\b\b\b\b/ thinking!\b\b\b\b\b\b\b\b\b\b\b- thinking!\b\b\b\b\b\b\b\b\b\b\b\\ thinking!\b\b\b\b\b\b\b\b\b\b\b| thinking!\b\b\b\b\b\b\b\b\b\b\b/ thinking!\b\b\b\b\b\b\b\b\b\b\b- thinking!\b\b\b\b\b\b\b\b\b\b\b\\ thinking!\b\b\b\b\b\b\b\b\b\b\b| thinking!\b\b\b\b\b\b\b\b\b\b\b/ thinking!\b\b\b\b\b\b\b\b\b\b\b- thinking!\b\b\b\b\b\b\b\b\b\b\b\\ thinking!\b\b\b\b\b\b\b\b\b\b\b| thinking!\b\b\b\b\b\b\b\b\b\b\b/ thinking!\b\b\b\b\b\b\b\b\b\b\b- thinking!\b\b\b\b\b\b\b\b\b\b\b\\ thinking!\b\b\b\b\b\b\b\b\b\b\b| thinking!\b\b\b\b\b\b\b\b\b\b\b/ thinking!\b\b\b\b\b\b\b\b\b\b\b- thinking!\b\b\b\b\b\b\b\b\b\b\b\\ thinking!\b\b\b\b\b\b\b\b\b\b\b| thinking!\b\b\b\b\b\b\b\b\b\b\b           \b\b\b\b\b\b\b\b\b\b\bAnswer: 42\n"
     ]
    }
   ],
   "source": [
    "# spinner_asyncio.py\n",
    "\n",
    "# credits: Example by Luciano Ramalho inspired by\n",
    "# Michele Simionato's multiprocessing example in the python-list:\n",
    "# https://mail.python.org/pipermail/python-list/2009-February/538048.html\n",
    "\n",
    "# BEGIN SPINNER_ASYNCIO\n",
    "import asyncio\n",
    "import itertools\n",
    "import sys\n",
    "\n",
    "\n",
    "@asyncio.coroutine  # <1>\n",
    "def spin(msg):  # <2>\n",
    "    write, flush = sys.stdout.write, sys.stdout.flush\n",
    "    for char in itertools.cycle('|/-\\\\'):\n",
    "        status = char + ' ' + msg\n",
    "        write(status)\n",
    "        flush()\n",
    "        write('\\x08' * len(status))\n",
    "        try:\n",
    "            yield from asyncio.sleep(.1)  # <3>\n",
    "        except asyncio.CancelledError:  # <4>\n",
    "            break\n",
    "    write(' ' * len(status) + '\\x08' * len(status))\n",
    "\n",
    "\n",
    "@asyncio.coroutine\n",
    "def slow_function():  # <5>\n",
    "    # pretend waiting a long time for I/O\n",
    "    yield from asyncio.sleep(3)  # <6>\n",
    "    return 42\n",
    "\n",
    "\n",
    "@asyncio.coroutine\n",
    "def supervisor():  # <7>\n",
    "    spinner = asyncio.async(spin('thinking!'))  # <8>\n",
    "    print('spinner object:', spinner)  # <9>\n",
    "    result = yield from slow_function()  # <10>\n",
    "    spinner.cancel()  # <11>\n",
    "    return result\n",
    "\n",
    "\n",
    "def main():\n",
    "    loop = asyncio.get_event_loop()  # <12>\n",
    "    result = loop.run_until_complete(supervisor())  # <13>\n",
    "    loop.close()\n",
    "    print('Answer:', result)\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n",
    "# END SPINNER_ASYNCIO\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
